package com.oasen.learn.usercenter.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.crypto.password.StandardPasswordEncoder;

import com.oasen.learn.usercenter.dao.UserAuthDao;
import com.oasen.learn.usercenter.dao.UserDao;
import com.oasen.learn.usercenter.service.UserMetaDetailsService;

@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
	
	@Autowired
    private UserDao userMetaDao;
	@Autowired
	private UserAuthDao userAuthDao;

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http
			.authorizeRequests().anyRequest().permitAll()
			.and()
			.formLogin().defaultSuccessUrl("/user")
			.and()
			.logout().logoutSuccessUrl("/")
			.and()
			.httpBasic();
	}
	
	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		auth
			.userDetailsService(new UserMetaDetailsService(userMetaDao, userAuthDao))
			.passwordEncoder(new StandardPasswordEncoder("53cr3t"));
	}
}
